package com.bwie.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.User;
import com.bwie.service.UserService;
import com.bwie.mapper.UserMapper;
import com.bwie.utils.R;
import com.bwie.vo.UserVo;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
* @author 61630
* @description 针对表【tb_user】的数据库操作Service实现
* @createDate 2024-08-06 14:04:25
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{
    @Resource
    private UserMapper userMapper;
    @Resource
    StringRedisTemplate stringRedisTemplate;
    @Override
    public R getLogin(UserVo userVo) {
        if(Objects.isNull(userVo.getUserName())){
            return R.error("账号为空");
        }
        if(Objects.isNull(userVo.getPassword())){
            return R.error("密码为空");
        }
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(User::getUserName,userVo.getUserName());
        User user = userMapper.selectOne(wrapper);
        if(Objects.isNull(user)){
            return R.error("账号有误");
        }
        if(!user.getPassword().equals(userVo.getPassword())){
            return R.error("密码错误");
        }
        if(Objects.isNull(userVo.getCode())){
            return R.error("验证码为空");
        }
        Boolean aBoolean = stringRedisTemplate.hasKey("user-" + userVo.getUserName());
        if(!aBoolean){
            return R.error("验证码不存在，请获取验证码");
        }
        String s = stringRedisTemplate.opsForValue().get("user-" + userVo.getUserName());
        if(!s.equals(userVo.getCode())){
             return R.error("验证码错误");
        }
        ValueOperations<String, String> opsForValue = stringRedisTemplate.opsForValue();
        opsForValue.set("userLogin:"+user.getId(),"",1, TimeUnit.MINUTES);
        String token = Jwts.builder().signWith(SignatureAlgorithm.HS256, "bwie").claim("id", user.getId()).claim("power", user.getPower()).compact();
        return R.success(token);
    }

    @Override
    public R sendCode(UserVo userVo) {
        if(userVo.getUserName().equals(null)){
            return R.error("账号为空");
        }
        ValueOperations<String, String> opsForValue = stringRedisTemplate.opsForValue();
        String s = RandomUtil.randomNumbers(4);
        opsForValue.set("user-"+userVo.getUserName(),s,1,TimeUnit.MINUTES);
        return R.success("验证码已发送请前往集群查看");
    }

    @Override
    public R findUser(UserVo userVo) {
        if(Objects.isNull(userVo.getUserName())){
            return R.error("账号为空");
        }
        if(Objects.isNull(userVo.getPassword())){
            return R.error("密码为空");
        }
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(User::getUserName,userVo.getUserName());
        User user = userMapper.selectOne(wrapper);
        if(Objects.isNull(user)){
            return R.error("账号有误");
        }

        if(Objects.isNull(userVo.getCode())){
            return R.error("验证码为空");
        }
        Boolean aBoolean = stringRedisTemplate.hasKey("user-" + userVo.getUserName());
        if(!aBoolean){
            return R.error("验证码不存在，请获取验证码");
        }
        String s = stringRedisTemplate.opsForValue().get("user-" + userVo.getUserName());
        if(!s.equals(userVo.getCode())){
            return R.error("验证码错误");
        }
        User newUser = new User();
        newUser.setId(user.getId());
        newUser.setUserName(user.getUserName());
        newUser.setPassword(userVo.getPassword());
        newUser.setPower(user.getPower());
        userMapper.updateById(newUser);
        return R.success("修改成功");
    }
}




